home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekkan Dennou Club 147
/
Gekkan Dennou Club - 2000.8 Vol. 147 (Japan).7z
/
Gekkan Dennou Club - 2000.8 Vol. 147 (Japan) (Track 1).bin
/
docs
/
perl
/
monarray.pl
next >
Wrap
Perl Script
|
2000-06-25
|
1KB
|
65 lines
# タイを使った配列の監視
package MonArray;
sub TIEARRAY {
my ($pkg, $name, $ra) = @_;
my $obj = {
name => $name, array => [@$ra],
};
bless $obj, $pkg;
$obj;
}
sub FETCH {
my ($obj, $index) = @_;
my $value = $obj->{'array'}->[$index];
print 'Read : $',$obj->{'name'}, "[$index] -> $value\n";
$value;
}
sub STORE {
my ($obj, $index, $new_value) = @_;
print 'Write : $',$obj->{'name'}, "[$index] <- $new_value\n";
$obj->{'array'}->[$index] = $new_value;
}
sub DESTROY {
my $obj = shift;
if( $obj->{'name'} ne '__UNMONITOR__' ){
print 'DESTORY @', "$obj->{'name'}\n";
}
}
sub unmonitor {
my ($pkg, $rvar) = @_;
my $last_array;
{
my $obj = tied @$rvar;
$last_array = $obj->{'array'};
$obj->{'name'} = '__UNMONITOR__'
}
untie @$rvar;
@$rvar = @$last_array;
}
package main;
@a = (10, 20, 30);
tie @a, 'MonArray', 'a', \@a;
$x = $a[1];
print "x = $x\n";
$a[1] = 200;
$y = $a[1];
print "y = $y\n";
MonArray->unmonitor( \@a );
print "@a\n";
# end of file